var Base64={_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:function(a){var c="",d,b,h,i,f,g,e=0;for(a=Base64._utf8_encode(a);e<a.length;){d=a.charCodeAt(e++);b=a.charCodeAt(e++);h=a.charCodeAt(e++);i=d>>2;d=(d&3)<<4|b>>4;f=(b&15)<<2|h>>6;g=h&63;if(isNaN(b))f=g=64;else if(isNaN(h))g=64;c=c+this._keyStr.charAt(i)+this._keyStr.charAt(d)+this._keyStr.charAt(f)+this._keyStr.charAt(g)}return c},decode:function(a){var c="",d,b,h,i,f,g,e=0;for(a=a.replace(/[^A-Za-z0-9\+\/\=]/g,
"");e<a.length;){h=this._keyStr.indexOf(a.charAt(e++));i=this._keyStr.indexOf(a.charAt(e++));f=this._keyStr.indexOf(a.charAt(e++));g=this._keyStr.indexOf(a.charAt(e++));d=(i&15)<<4|f>>2;b=(f&3)<<6|g;c+=String.fromCharCode(h<<2|i>>4);if(f!=64)c+=String.fromCharCode(d);if(g!=64)c+=String.fromCharCode(b)}return c=Base64._utf8_decode(c)},_utf8_encode:function(a){a=a.replace(/\r\n/g,"\n");for(var c="",d=0;d<a.length;d++){var b=a.charCodeAt(d);if(b<128)c+=String.fromCharCode(b);else{if(b>127&&b<2048)c+=
String.fromCharCode(b>>6|192);else{c+=String.fromCharCode(b>>12|224);c+=String.fromCharCode(b>>6&63|128)}c+=String.fromCharCode(b&63|128)}}return c},_utf8_decode:function(a){for(var c="",d=0,b=c1=c2=0;d<a.length;){b=a.charCodeAt(d);if(b<128){c+=String.fromCharCode(b);d++}else if(b>191&&b<224){c2=a.charCodeAt(d+1);c+=String.fromCharCode((b&31)<<6|c2&63);d+=2}else{c2=a.charCodeAt(d+1);c3=a.charCodeAt(d+2);c+=String.fromCharCode((b&15)<<12|(c2&63)<<6|c3&63);d+=3}}return c}};

function properCase(s){return s.toLowerCase().replace(/^(.)|\s(.)/g,function($1){return $1.toUpperCase();});}

// Time 
var cancelreasons = ['Justified','Not Justified','Professional'];
var durations = [15,30,45,60,75,90,10,120,135,150,165,180];
//var durations = [30,45,60,75,90,10,120];

//Zero it
var L2 = function(v){return v<10 ? '0' + v : v;}
var L3 = function(v){return v<100 ? '0' + v : v;}

function getAsTimeFormat(duration)
{
	// from 15 minutes up to 23 hours
	var minutes = parseInt(duration);
	if (minutes >=15 && minutes <= 1380){
		var hms = getAsTimeObject(minutes);
		return L2(hms.hour)+":"+L2(hms.minute);
	}
	return "00:45";
};

function getAsTimeObject(minutes)
{
	if (!minutes) return {hour: 0 , minute:  45, second: 0};
	var duration = parseInt(minutes);
	if (duration<60)
		return {hour: 0 , minute:  duration, second: 0};
	else
		if (duration%60==0)
			return {hour: duration/60 , minute: 0, second: 0};
		else
			return {hour: parseInt(duration / 60), minute: duration % 60, second: 0};
};

/*
  ex:
    startTime = {year: 2010, month:11, date: 13, hour: 3, minute: 45, second: 10}
	interval = {hour: 3, minute: 45, second: 10}
	
	return:
	
*/
function getEndTime(startTime, minutes)
{
	var duration = (minutes || '45');
	var interval = getAsTimeObject(duration);
  
	var hour  = parseInt(interval.hour)   + startTime.hour;
	var mins  = parseInt(interval.minute) + startTime.minute;
	var secs  = parseInt(interval.second) + startTime.second;

	if (secs>=60) { secs = -(60 - secs); mins = parseInt(mins)+1; }
	if (mins>=60) { mins = -(60 - mins); hour = parseInt(hour)+1; }
	if (hour>=24) { hour = -(24 - hour); }

	return {
		year:		startTime.year,
		month: 		startTime.month,
		date: 		startTime.date,
		hour: 		hour,
		minute: 	mins,
		second: 	secs
	};
}
// GData definitions
function getPhoneType(info){
	var types =	{
		"http://schemas.google.com/g/2005#assistant":		"assistant", 		
		"http://schemas.google.com/g/2005#callback":        "callback", 		
		"http://schemas.google.com/g/2005#car":             "car", 			
		"http://schemas.google.com/g/2005#company_main":    "company_main", 	
		"http://schemas.google.com/g/2005#fax":             "fax",			
		"http://schemas.google.com/g/2005#home":            "home", 			
		"http://schemas.google.com/g/2005#home_fax":        "home_fax", 		
		"http://schemas.google.com/g/2005#isdn":            "isdn", 			
		"http://schemas.google.com/g/2005#main":            "main", 			
		"http://schemas.google.com/g/2005#mobile":          "mobile", 		
		"http://schemas.google.com/g/2005#other":           "other", 			
		"http://schemas.google.com/g/2005#other_fax":       "other_fax", 		
		"http://schemas.google.com/g/2005#pager":           "pager", 			
		"http://schemas.google.com/g/2005#radio":           "radio", 			
		"http://schemas.google.com/g/2005#telex":           "telex", 			
		"http://schemas.google.com/g/2005#tty_tdd":         "tty_tdd", 		
		"http://schemas.google.com/g/2005#work":            "work", 			
		"http://schemas.google.com/g/2005#work_fax":        "work_fax", 		
		"http://schemas.google.com/g/2005#work_mobile":     "work_mobile", 	
		"http://schemas.google.com/g/2005#work_pager":      "work_pager"
	};
	
	if (info.substring(0,4)=='http'){
		if (info in types)
			return types[info];
		else
			return 'home';
	}
	else{
		for(var key in types){
		  if (types[key]==info)
			 return key;
		}
		return "http://schemas.google.com/g/2005#home";
	}
}

function getGadgetURL()
{
	var url = (window.document.location.search ? window.document.location.search : '');
	if (url.length>0){
		url = url.replace(/\?url\=/ig,'');
		var asArray= url.split('xml');
		if (asArray.length>0)
			url = asArray[0]+'.xml';
	};
	return url;
};

function setExtendedProperty(entry,propname,propvalue)
{
	// Remove any existing extended properties
	var properties = entry.getExtendedProperties();
	if (properties && properties.length>0){
		for(var j=properties.length-1;j>=0;j--){
			if (properties[j].getName()===propname){
				properties.splice(j,1);
				break;
			}
		}
	}	
	// Create a new one
	var prop = null;
	prop=new google.gdata.ExtendedProperty();
	prop.setName(propname);
	prop.setValue(propvalue);
	properties.push(prop);
	// set then it
	entry.setExtendedProperties(properties);
}

function getExtendedProperty(entry,propname)
{
	var properties = entry.getExtendedProperties();
	if (properties && properties.length>0){
		for(var j=0;j<properties.length;j++){
			if (properties[j].getName()===propname){
				return properties[j].getValue();
			}
		}
	};
	return null;
}

function gdataid(href)
{
	var gdataid = href.split('/');
	return gdataid[gdataid.length-1];
}

function _elID(entry,tabid,suffix)
{
	var ctrlid = gdataid(entry.id.$t);
	var ret = 'T'+tabid+'_'+ctrlid+'_'+suffix;
	return ret;
}


// OAuth Dance routines

var popup_;
function getOAuthUrlSuffix() {
  var user = 'default';
  var params = gadgets.util.getUrlParameters();
  if (params && params.parent) {
    var HOSTED = '/hosted/';
    var hosted = params.parent.indexOf(HOSTED);
    if (hosted != -1) {
      user = params.parent.substr(hosted + HOSTED.length);
    }
    return '&hd=' + escape(user);
  }
  return '';
}

function makeCallback(func, originalFunc) {
  return function(response) {
    if (response && response.oauthApprovalUrl) {
      popup_ = new gadgets.oauth.Popup(
        response.oauthApprovalUrl + getOAuthUrlSuffix(), 'height=600,width=800',
        function() {
          show('<div style="white-space: normal;font-size: 0.75em;margin: 3px 3px;padding: 5px 5px;width: 150px;">' + 
			   'Please go to the popup and click "Grant access". ' +
               'Then click this button to continue.<br><br><button ' +
               'onclick="popup_.createApprovedOnClick()()" style="width:130px;margin:2px 2px; padding: 1px 1px;white-space:normal;">' +
               'I\'ve approved access</button>');
        },
        originalFunc
      );
      show('<button style="width:150px;margin:2px 2px; padding: 1px 1px;white-space:normal;" ' +
           'onclick="popup_.createOpenerOnClick()()">' +
           'Please press this button to give the ' +
           'gadget permission to requested resources.' +
           '</button>');
    } 
	else if (!func(response)) {
      showWarning('Read Error');
    }
  };
}